var t = require("../../@babel/runtime/helpers/interopRequireDefault")(require("../../@babel/runtime/regenerator")),
  a = require("../../@babel/runtime/helpers/asyncToGenerator"),
  i = require("../../@babel/runtime/helpers/objectSpread2"),
  e = require("../../utils/mapList"),
  n = require("../../utils/borderList"),
  s = require("../../utils/config"),
  o = require("../../utils/util"),
  r = (require("./glsl"), require("../../utils/login")),
  d = require("../../utils/api"),
  l = getApp();
Page({
  data: {
    mapList: e.list,
    image: "".concat(s.imgPrefix, "/static/assets/bg/bg-new19.jpg"),
    imageWidth: 2964,
    imageHeight: 5e3,
    scale: 1,
    styleString: "",
    styleHeight: "",
    canvas: null,
    ctx: null,
    scrollTop: 0,
    scrollLeft: 0,
    motto: "",
    isLogin: !0,
    isRender: !0,
    isShowRenderBg: !0,
    isShowMap: !1,
    channel: "",
    landActiveImgMask: "",
    landCanvasImage: "",
    bgImage: "".concat(s.imgPrefix, "/static/assets/loading_bg.png"),
    msgBoxClassName: "right",
    guideImage: "".concat(s.imgPrefix, "/static/assets/icon_hand.png"),
    guideMoveImage: "".concat(s.imgPrefix, "/static/assets/icon_hand_move.png"),
    bgMsgBox: "".concat(s.imgPrefix, "/static/assets/bg_msg_box.png"),
    land: {
      landMsgImg: "".concat(s.imgPrefix, "/static/assets/land-msg/img_msg_new_1.png"),
      logoImg: "".concat(s.imgPrefix, "/static/assets/logo_YS.png"),
      landActiveImgMask: "",
      channel: "",
      status: "FarmStatus_Online",
      v_quota: 1e4,
      sold_quota: 0,
      price_rmb_fen: 0
    },
    inputNum: 10,
    maxNum: 100,
    isBlank: !1,
    isSubmitShow: !1,
    isMsgShow: !1,
    isShowBack: !1,
    isShowClose: !1,
    isGuideShow: !1,
    showGuideHand: !0,
    navBar: {
      statusBarHeight: 0,
      navigationBarHeight: 0
    },
    isLandAudioPlay: !1,
    isLandAudioLoading: !1,
    scrollAnimation: !1,
    bounces: !1,
    timer: null,
    showHandAn: !1,
    windowWidth: 0
  },
  onLoad: function (t) {
    var a = this;
    if (setTimeout((function () {
        console.log("load"), a.setData({
          showHandAn: !0
        })
      }), 100), !wx.getStorageSync("token")) return this.setData({
      isLogin: !1
    });
    this.setData({
      isLogin: !0
    }), wx.onMemoryWarning((function () {
      console.log("onMemoryWarningReceive-----")
    }))
  },
  initC: function () {
    var t = this;
    wx.createSelectorQuery().select("#myCanvas").fields({
      node: !0,
      size: !0
    }).exec((function (a) {
      var i = a[0].node,
        e = i.getContext("2d");
      i.width = a[0].width, i.height = a[0].height, t.setData({
        canvas: i,
        ctx: e
      })
    }))
  },
  getPoint: function (t) {
    var a = Number(this.data.windowWidth) / 2 > t.touches[0].pageX ? "right" : "left";
    this.setData({
      msgBoxClassName: a
    })
  },
  onTouchStart: function (t) {
    if (console.log(t, "[ppppp"), !this.data.isMsgShow && !this.data.isGuideShow) {
      var a = this.findClick(t.touches[0].x, t.touches[0].y);
      if (this.data.ctx.globalCompositeOperation = "source-over", this.data.ctx.clearRect(0, 0, this.data.canvas.width, this.data.canvas.height), a || 0 === a) {
        console.log(this.data.land), this.drawLine(this.data.mapList[a].border);
        for (var e = null, n = 0, s = Object.keys(this.landData); n < s.length; n++) {
          var o = s[n];
          if (this.landData[o].code === this.data.mapList[a].code) {
            e = o;
            break
          }
        }
        console.log(this.landData, e, "pp"), this.land = {
          audio: null
        }, this.setData({
          land: i({}, this.landData[e]),
          isMsgShow: !0,
          isShowClose: !0
        })
      }
    }
  },
  findClick: function (t, a) {
    if (!this.data.isGuideShow)
      for (var i = 0; i < this.data.mapList.length; i++)
        if (this.drawLine(this.data.mapList[i].border), this.data.ctx.isPointInPath(t, a)) return i
  },
  drawLine: function (t) {
    this.data.ctx.fillStyle = "rgba(0,0,0,0.3)", this.data.ctx.fillRect(0, 0, this.data.canvas.width, this.data.canvas.height), this.data.ctx.beginPath(), this.data.ctx.moveTo(t[0].split(",")[0] * this.data.scale, t[0].split(",")[1] * this.data.scale);
    for (var a = 1; a < t.length; a++) this.data.ctx.lineTo(t[a].split(",")[0] * this.data.scale, t[a].split(",")[1] * this.data.scale);
    this.data.ctx.closePath(), this.data.ctx.globalCompositeOperation = "destination-out", this.data.ctx.fillStyle = "rgba(0,0,0,1)", this.data.ctx.fill()
  },
  drawWhiteLine: function () {
    var t = this;
    this.data.ctx.lineWidth = 3, this.data.ctx.strokeStyle = "rgba(255,255,255,0.5)", this.whiteLine(), setTimeout((function () {
      t.data.ctx.strokeStyle = "rgba(255,255,255,0.6)", t.whiteLine()
    }), 1e3)
  },
  whiteLine: function () {
    if (this.data.isGuideShow) {
      this.data.ctx.clearRect(0, 0, this.data.canvas.width, this.data.canvas.height);
      for (var t = 2.5 * this.data.scale, a = 0; a < n.borderList.length; a++) {
        var i = n.borderList[a].border;
        this.data.ctx.beginPath(), this.data.ctx.moveTo(i[0].split(",")[0] * t, i[0].split(",")[1] * t);
        for (var e = 1; e < i.length; e++) this.data.ctx.lineTo(i[e].split(",")[0] * t, i[e].split(",")[1] * t);
        this.data.ctx.closePath(), this.data.ctx.stroke()
      }
    }
  },
  onReady: function () {},
  onShow: function () {
    var t = this;
    return console.log("show"), l.globalData.isShoHide ? l.globalData.isShoHide = !1 : "" == l.globalData.userPageFrom && (console.log("show-land"), this.isRender = !1, this.showMaskAnimation = !1, this.showMoveAnimation = !1, this.isGuide = wx.getStorageSync("isGuide"), this.isReload = !1, this.setTopBar(), this.setData({
      showHandAn: !1
    }), void setTimeout((function () {
      t.showLand()
    }), 50))
  },
  setTopBar: function () {
    var t = wx.getStorageSync("statusBarHeight"),
      a = wx.getStorageSync("navigationBarHeight"),
      i = {
        statusBarHeight: t,
        navigationBarHeight: a,
        backTop: t + a - (0, o.createRpx2px)(34) - 5
      };
    console.log("navBar-----", i), this.setData({
      navBar: i
    })
  },
  showLand: function () {
    var i = this;
    return a(t.default.mark((function a() {
      var e, n, s;
      return t.default.wrap((function (t) {
        for (;;) switch (t.prev = t.next) {
          case 0:
            e = wx.getSystemInfoSync(), n = e.windowWidth, s = e.windowHeight, i.setData({
              windowWidth: n
            }), i.init(n, s), console.log(n, s);
          case 4:
          case "end":
            return t.stop()
        }
      }), a)
    })))()
  },
  filterLandData: function (e) {
    return a(t.default.mark((function n() {
      var o, r;
      return t.default.wrap((function (n) {
        for (;;) switch (n.prev = n.next) {
          case 0:
            return o = {}, r = Object.keys(e), n.next = 4, r.forEach(function () {
              var n = a(t.default.mark((function a(n) {
                var r, d, l, c, u, h;
                return t.default.wrap((function (t) {
                  for (;;) switch (t.prev = t.next) {
                    case 0:
                      (r = s.araeCodeStr["".concat(e[n].code)]) && (d = "".concat(s.imgPrefix, "/static/assets/land-active/land_active_mask_").concat(r, ".png"), l = "".concat(s.imgPrefix, "/static/assets/land-active/land-new/").concat(r, ".jpg"), c = "".concat(s.imgPrefix, "/static/assets/land-msg/new_03/img_msg_new_").concat(r, ".png"), u = e[n].sold_quota / e[n].v_quota * 100, h = "".concat(s.imgPrefix, "/static/assets/land-audio/audio_msg_").concat(r, ".mp3"), o[r] = i(i({}, e[n]), {}, {
                        baseAraeRGB: s.baseArae[r].araeRGB,
                        landActiveImgMask: d,
                        landActiveImg: l,
                        landMsgImg: c,
                        progress: u,
                        audioSrc: h
                      }));
                    case 2:
                    case "end":
                      return t.stop()
                  }
                }), a)
              })));
              return function (t) {
                return n.apply(this, arguments)
              }
            }());
          case 4:
            return n.abrupt("return", o);
          case 5:
          case "end":
            return n.stop()
        }
      }), n)
    })))()
  },
  getLandData: function () {
    var i = this;
    return a(t.default.mark((function a() {
      var e, n, s;
      return t.default.wrap((function (t) {
        for (;;) switch (t.prev = t.next) {
          case 0:
            return wx.showLoading({
              title: "前往何家岩农田",
              mask: !0
            }), console.log("fromArea:", l.globalData.fromArea), e = {}, l.globalData.fromArea ? (e.from = l.globalData.fromArea, e.ret_from = 1) : (e.from = "", e.ret_from = 0), t.prev = 4, t.next = 7, (0, d.getFarmsAPI)(e);
          case 7:
            n = t.sent, t.next = 19;
            break;
          case 10:
            if (t.prev = 10, t.t0 = t.catch(4), console.log(t.t0), wx.hideLoading(), 401 != t.t0.statusCode) {
              t.next = 16;
              break
            }
            return t.abrupt("return", t.t0.data);
          case 16:
            if (!t.t0.meta || 401 != t.t0.meta.code) {
              t.next = 18;
              break
            }
            return t.abrupt("return", {
              msg: "must login"
            });
          case 18:
            return t.abrupt("return", !1);
          case 19:
            return s = i.filterLandData(n.areas), t.abrupt("return", s);
          case 21:
          case "end":
            return t.stop()
        }
      }), a, null, [
        [4, 10]
      ])
    })))()
  },
  guideHideHandler: function () {
    this.data.timer && clearInterval(this.data.timer), this.setData({
      isGuideShow: !1,
      timer: null
    })
  },
  init: function (i, e) {
    var n = this;
    return a(t.default.mark((function a() {
      var e;
      return t.default.wrap((function (t) {
        for (;;) switch (t.prev = t.next) {
          case 0:
            return n.data.timer && clearInterval(n.data.timer), n.setData({
              isGuideShow: !1,
              timer: null
            }), t.next = 4, n.getLandData();
          case 4:
            if (n.landData = t.sent, n.landData) {
              t.next = 7;
              break
            }
            return t.abrupt("return", wx.showToast({
              title: "获取农田数据失败！",
              icon: "none",
              duration: 1e3
            }));
          case 7:
            if (!n.landData.msg) {
              t.next = 9;
              break
            }
            return t.abrupt("return", wx.showToast({
              title: "领养前请先登录！",
              icon: "error",
              duration: 1500,
              mask: !0,
              success: function () {
                wx.getStorageSync("token") && wx.removeStorageSync("token"), n.setData({
                  isLogin: !1
                })
              }
            }));
          case 9:
            n.setData({
              channel: l.globalData.fromArea
            }), console.log("data.channel", n.data.channel), console.log(n.landData, "pp"), e = 1.8 * i / n.data.imageWidth, n.setData({
              scale: e,
              styleString: "height:" + n.data.imageHeight * e + "px;width:" + n.data.imageWidth * e + "px",
              styleHeight: "height:" + n.data.imageHeight * e + "px"
            }), n.setData({
              isShowMap: !0
            }), n.initC(), console.log(n.data.showHandAn), n.data.showHandAn && (n.setData({
              scrollTop: Math.floor(200 * e)
            }), setTimeout((function () {
              var t = Math.floor(450 * e),
                a = 0;
              n.setData({
                isGuideShow: !0,
                showGuideHand: !0
              });
              var i = setInterval((function () {
                a <= t ? (a += 1, n.setData({
                  scrollLeft: a
                })) : (clearInterval(i), n.setData({
                  showGuideHand: !1
                }), n.data.isGuideShow && n.setData({
                  showGuideHand: !1
                }))
              }), 10)
            }), 100)), n.isRender = !0, n.setData({
              isShowRenderBg: !1
            }), wx.hideLoading({});
          case 21:
          case "end":
            return t.stop()
        }
      }), a)
    })))()
  },
  loginHandler: function () {
    var i = this;
    return a(t.default.mark((function a() {
      return t.default.wrap((function (t) {
        for (;;) switch (t.prev = t.next) {
          case 0:
            return t.next = 2, (0, r.wxlogin)();
          case 2:
            if (t.sent) {
              t.next = 5;
              break
            }
            return t.abrupt("return", !1);
          case 5:
            i.setData({
              isLogin: !0
            }), i.showLand();
          case 7:
          case "end":
            return t.stop()
        }
      }), a)
    })))()
  },
  closeHandler: function (t) {
    this.setData({
      isMsgShow: !1,
      isShowClose: !1,
      isLandAudioPlay: !1,
      isLandAudioLoading: !1
    }), this.land.audio && (this.land.audio.destroy(), this.land.audio = null), this.data.ctx.clearRect(0, 0, this.data.canvas.width, this.data.canvas.height)
  },
  backHandler: function () {
    this.setData({
      isSubmitShow: !1,
      isBlank: !1,
      isShowBack: !1,
      isShowClose: !0
    }), this.isReload && (this.onHide(), this.showLand())
  },
  createLandAudio: function () {
    var t = this;
    this.land.audio ? this.landAudioState(!this.data.isLandAudioPlay) : (console.log("create audio"), this.setData({
      isLandAudioLoading: !0
    }), this.land.audio = wx.createInnerAudioContext(), this.land.audio.src = this.data.land.audioSrc, this.land.audio.onCanplay((function () {
      console.log("audio canplay!"), t.land.audio.play(), t.land.audio.offCanplay()
    })), this.land.audio.onEnded((function () {
      t.setData({
        isLandAudioPlay: !1
      })
    })), this.land.audio.onTimeUpdate((function () {
      console.log(t.land.audio.currentTime), t.land.audio.currentTime >= .3 && (t.setData({
        isLandAudioLoading: !1,
        isLandAudioPlay: !0
      }), t.land.audio.offTimeUpdate())
    })))
  },
  landAudioState: function (t) {
    if (this.data.isLandAudioLoading) return !1;
    t ? (console.log("audio play!"), this.land.audio.play()) : this.land.audio.pause(), this.setData({
      isLandAudioPlay: t
    })
  },
  confirmHandler: function () {
    this.setData({
      isSubmitShow: !0,
      isBlank: !0,
      isShowBack: !0,
      isShowClose: !1,
      inputNum: 10,
      isLandAudioPlay: !1,
      isLandAudioLoading: !1
    }), this.land.audio && (this.land.audio.destroy(), this.land.audio = null);
    try {
      wx.reportAnalytics("btn_land_select", {
        title: this.data.land.title
      })
    } catch (t) {
      console.log(t)
    }
  },
  submitHandler: function () {
    var t = this;
    if (console.log("submit!"), this.data.inputNum < this.data.land.buy_min_limit) return wx.showToast({
      title: "最少认养".concat(this.data.land.buy_min_limit, "平米！"),
      icon: "none",
      duration: 1500,
      mask: !0
    });
    console.log("num:", this.data.inputNum), wx.showLoading({
      title: "订单锁定中",
      mask: !0
    });
    var a = (0, o.random)(s.randomShareNum.min, s.randomShareNum.max);
    console.log(s.randomShareNum.min, s.randomShareNum.max, a, "-====");
    "".concat(s.cdnImgPrefix, "/share-img/share_").concat(a, ".jpg");
    var e = {
        bgName: a,
        bgCoverName: a,
        from: l.globalData.fromArea
      },
      n = JSON.stringify(e);
    (0, d.preOrderLockAPI)({
      farm_id: this.data.land.id,
      quota: this.data.inputNum,
      share_tpl: n
    }).then((function (a) {
      console.log("Lockres:", a);
      wx.navigateTo({
        url: "./land-order/index",
        success: function (e) {
          e.eventChannel.emit("acceptDataFromOpenerPage", i(i(i({}, t.data.land), a), {}, {
            inputNum: t.data.inputNum,
            addr: null,
            share_img: n
          }))
        }
      })
    })).catch((function (a) {
      return console.log(a), wx.hideLoading(), a.data && -1 !== a.data.msg.indexOf("请求太频繁") ? wx.showToast({
        title: "请求太频繁",
        icon: "error",
        duration: 1e3
      }) : a.errorMsg && "1004" == a.errorMsg ? (t.isReload = !0, t.data.land.v_quota - t.data.land.sold_quota > 0 ? wx.showToast({
        title: "库存紧张请稍后",
        icon: "error",
        duration: 1e3
      }) : wx.showToast({
        title: "农田已售完！",
        icon: "error",
        duration: 1e3
      })) : wx.showToast({
        title: "订单锁定失败！",
        icon: "error",
        duration: 1e3
      })
    }));
    try {
      wx.reportAnalytics("btn_land_submit", {
        title: this.data.land.title
      })
    } catch (t) {}
  },
  numAddHandler: function () {
    var t = this.data.inputNum;
    t += 10, this.setInputNun(t)
  },
  numSubHandler: function () {
    var t = this.data.inputNum;
    t -= 10, this.setInputNun(t)
  },
  keyInputHandler: function (t) {
    this.setInputNun(Number(t.detail.value))
  },
  setInputNun: function (t) {
    var a = this.data.land.buy_max_limit;
    t > a && (t = a, wx.showToast({
      title: "一次最多购买".concat(a, "平米！"),
      icon: "none",
      duration: 1500,
      mask: !0
    })), t > this.data.maxNum && (t = this.data.maxNum), t <= 0 && (t = 0), this.setData({
      inputNum: Number(t)
    })
  },
  onHide: function () {
    if (this.data.ctx.clearRect(0, 0, this.data.canvas.width, this.data.canvas.height), console.log("land hide"), l.globalData.isShoHide) return !1;
    this.land && this.land.audio && this.land.audio.destroy(), this.setData({
      isBlank: !1,
      isSubmitShow: !1,
      isMsgShow: !1,
      isShowBack: !1,
      isShowClose: !1,
      isLogin: !0,
      isShowRenderBg: !0,
      isGuideShow: !1,
      isLandAudioPlay: !1,
      ctx: null,
      canvas: null
    }), wx.setStorageSync("isGuide", !1)
  },
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {}
});